package com.zyx.yygh.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyx.yygh.common.result.Result;
import com.zyx.yygh.common.util.MD5;
import com.zyx.yygh.hosp.service.HospitalSetService;
import com.zyx.yygh.model.hosp.HospitalSet;
import com.zyx.yygh.vo.hosp.HospitalSetQueryVo;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

/**
 * @author zyx
 * @version 1.0
 * @date 2022/1/19 019 19:55
 */
//@CrossOrigin
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

    @Resource
    private HospitalSetService hospitalSetService;

    /**
     * 查询所有信息
     *
     * @return
     */
    @GetMapping("findAll")
    public Result findAllHospital() {

        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    /**
     * 逻辑删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public Result removeHospSet(@PathVariable long id) {
        boolean flag = hospitalSetService.removeById(id);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    /**
     * 条件查询带分页
     */
    @PostMapping("findPageHospital/{current}/{limit}")
    public Result findPageHospital(@PathVariable long current,
                                   @PathVariable long limit,
                                   @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {

        Page<HospitalSet> tPage = new Page<>(current, limit);
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        String hosname = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();
        if (!StringUtils.isEmpty(hosname)) {
            wrapper.like("hosname", hospitalSetQueryVo.getHosname());
        }
        if (!StringUtils.isEmpty(hoscode)) {
            wrapper.eq("hoscode", hospitalSetQueryVo.getHoscode());
        }

        Page<HospitalSet> pageHospitalSet = hospitalSetService.page(tPage, wrapper);

        return Result.ok(pageHospitalSet);
    }

    /**
     * 添加医院设置
     */
    @PostMapping("saveHospital")
    public Result saveHospital(@RequestBody HospitalSet hospitalSet) {

        hospitalSet.setStatus(1);
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));

        boolean save = hospitalSetService.save(hospitalSet);
        if (save){
            return Result.ok();
        }else {
            return Result.fail();
        }

    }

    /**
     * 根据id 获取医院设置
     */
    @GetMapping("getHospSet/{id}")
    public Result getHospSet(@PathVariable long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    /**
     * 修改医院设置
     */
    @PostMapping("updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet){

        boolean b = hospitalSetService.updateById(hospitalSet);
        if (b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    /**
     * 批量删除医院
     */
    @DeleteMapping("batchRemove")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> idList){

        hospitalSetService.removeByIds(idList);
        return Result.ok();
    }

    /**
     * 医院设置的锁定和解锁
     */
    @PutMapping("lockHospital/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,
                                  @PathVariable Integer status){

        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);

        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }

    /**
     * 发送签名密钥
     */
    @PutMapping("sendKey/{id}")
    public Result sendKey(@PathVariable Long id){

        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //TODO 发送短信
        return Result.ok();
    }
}
